<template>
  <!--历史病历列表 -->
  <a-spin :spinning="loading">
    <a-collapse v-if="dataSource.length > 0">
      <a-collapse-panel v-for="(list, listIndex) in dataSource" :key="listIndex">
        <template #header>
          <!-- <div class="flex justify-between">
            <LabelValue class="mb-1" :value="list?.empName" />
          </div> -->
          <div class="flex mb-1">
            <LabelValue :width="138" :value="list?.jzsj" />
            <LabelValue :width="80" :value="list?.ksmc" />
          </div>
          <div class="flex">诊断： <LabelValue class="mb-1" :value="list?.bcLczdZdmc" /></div>
        </template>
        <div>
          <BaseTitle title="病历">
            <!-- :disabled="!list?.bcZs" -->
            <a-button type="primary" size="small" ghost @click="onCopy(1, list)">复制</a-button>
          </BaseTitle>
          <div>
            <!-- 西医病历 -->
            <template v-if="list?.mrmCode === 'XYBL'">
              <LabelValue class="mb-3" label="主诉" width="200" tooltip :value="list?.bcZs" />
              <LabelValue class="mb-3" label="病史" width="200" tooltip :value="list?.bcXbs" />
              <LabelValue class="mb-3" label="体查" width="200" tooltip :value="list?.bcTgjc" />
              <LabelValue class="mb-3" label="辅查" width="200" tooltip :value="list?.bcFzjc" />
              <LabelValue class="mb-3" label="初步诊断" width="200" tooltip :value="list?.bcLczdZdmc" />
              <LabelValue class="mb-3" label="其他诊断" width="200" tooltip :value="list?.bcQtzdZdmc" />
              <LabelValue class="mb-3" label="注意事项" width="200" tooltip :value="list?.bcZysx" />
            </template>
            <!-- 中医 -->
            <template v-else-if="list?.mrmCode === 'ZYBL'">
              <LabelValue class="mb-3" label="主诉" width="200" tooltip :value="list?.bcZs" />
              <LabelValue class="mb-3" label="现病史" width="200" tooltip :value="list?.bcXbs" />
              <LabelValue class="mb-3" label="既往史" width="200" tooltip :value="list?.bcJws" />
              <LabelValue class="mb-3" label="舌质" width="200" tooltip :value="list?.zybcSz" />
              <LabelValue class="mb-3" label="舌苔" width="200" tooltip :value="list?.zybcSt" />
              <LabelValue class="mb-3" label="脉象" width="200" tooltip :value="list?.zybcMx" />
              <LabelValue class="mb-3" label="阳性体质" width="200" tooltip :value="list?.zybcYxtz" />
              <LabelValue class="mb-3" label="辅助检查" width="200" tooltip :value="list?.bcFzjc" />
              <LabelValue class="mb-3" label="辩证施治" width="200" tooltip :value="list?.zybcBzsz" />
              <LabelValue class="mb-3" label="中医诊断" width="200" tooltip :value="list?.zybcZyzdZdmc" />
              <LabelValue class="mb-3" label="中医证名" width="200" tooltip :value="list?.zybcZyzxMc" />
              <LabelValue class="mb-3" label="临床诊断" width="200" tooltip :value="list?.bcLczdZdmc" />
            </template>
            <!-- 眼科病历一 -->
            <template v-else-if="list?.mrmCode === 'YKBL1'">
              <LabelValue class="mb-3" label="主诉" width="200" tooltip :value="list?.bcZs" />
              <LabelValue class="mb-3" label="现病史" width="200" tooltip :value="list?.bcXbs" />
              <LabelValue class="mb-3" label="既往史" width="200" tooltip :value="list?.bcJws" />
              <LabelValue class="mb-3" label="个人史" width="200" tooltip :value="list?.bcGrs" />
              <LabelValue class="mb-3" label="家族史" width="200" tooltip :value="list?.bcJzs" />
              <LabelValue class="mb-3" label="过敏史" width="200" tooltip :value="list?.bcGms" />
              <!-- <LabelValue class="mb-3" label="眼部检查" :value="list?.bcYbjc" /> -->
              <LabelValue class="mb-3" label="辅助检查" width="200" tooltip :value="list?.bcFzjc" />
              <LabelValue class="mb-3" label="初步诊断" width="200" tooltip :value="list?.bcLczdZdmc" />
              <LabelValue class="mb-3" label="其他诊断" width="200" tooltip :value="list?.bcQtzdZdmc" />
              <LabelValue class="mb-3" label="处理意见" width="200" tooltip :value="list?.bcCl" />
              <!-- <LabelValue class="mb-3" label="交待" width="200" tooltip :value="list?.bcJd" /> -->
            </template>
            <!-- 眼科病历二 -->
            <template v-else-if="list?.mrmCode === 'YKBL2'">
              <LabelValue class="mb-3" label="主诉" width="200" tooltip :value="list?.bcZs" />
              <LabelValue class="mb-3" label="现病史" width="200" tooltip :value="list?.bcXbs" />
              <LabelValue class="mb-3" label="既往史" width="200" tooltip :value="list?.bcJws" />
              <LabelValue class="mb-3" label="过敏史" width="200" tooltip :value="list?.bcGms" />
              <!-- <LabelValue class="mb-3" label="眼部检查" :value="list?.bcYbjc" /> -->
              <LabelValue class="mb-3" label="初步诊断" width="200" tooltip :value="list?.bcLczdZdmc" />
              <LabelValue class="mb-3" label="其他诊断" width="200" tooltip :value="list?.bcQtzdZdmc" />
              <LabelValue class="mb-3" label="处理意见" width="200" tooltip :value="list?.bcCl" />
              <!-- <LabelValue class="mb-3" label="交待" width="200" tooltip :value="list?.bcJd" /> -->
            </template>
            <!-- 眼科病历三 -->
            <template v-else-if="list?.mrmCode === 'YKBL3'">
              <LabelValue class="mb-3" label="主诉" width="200" tooltip :value="list?.bcZs" />
              <LabelValue class="mb-3" label="现病史" width="200" tooltip :value="list?.bcXbs" />
              <LabelValue class="mb-3" label="既往史" width="200" tooltip :value="list?.bcJws" />
              <!-- <LabelValue class="mb-3" label="个人史" width="200" tooltip :value="list?.bcGrs" />
              <LabelValue class="mb-3" label="家族史" width="200" tooltip :value="list?.bcJzs" /> -->
              <LabelValue class="mb-3" label="过敏史" width="200" tooltip :value="list?.bcGms" />
              <!-- <LabelValue class="mb-3" label="眼部检查" :value="list?.bcYbjc" /> -->
              <LabelValue class="mb-3" label="辅助检查" width="200" tooltip :value="list?.bcFzjc" />
              <LabelValue class="mb-3" label="初步诊断" width="200" tooltip :value="list?.bcLczdZdmc" />
              <LabelValue class="mb-3" label="其他诊断" width="200" tooltip :value="list?.bcQtzdZdmc" />
              <LabelValue class="mb-3" label="处理意见" width="200" tooltip :value="list?.bcCl" />
              <!-- <LabelValue class="mb-3" label="交待" width="200" tooltip :value="list?.bcJd" /> -->
            </template>
          </div>
          <BaseTitle title="处置">
            <a-button :disabled="!list.dispose.length > 0" type="primary" size="small" ghost @click="onCopy(2, list)">复制</a-button>
          </BaseTitle>
          <div v-if="list.dispose">
            <template v-for="(item, indexDispose) in list.dispose" :key="indexDispose">
              <a-divider v-if="indexDispose > 0" class="my-2" dashed />
              <div class="flex justify-between items-center">
                <LabelValue class="mb-3" :value="item?.mc" />
                <span>x{{ item?.sl }}</span>
              </div>
              <div>
                <LabelValue class="mb-3" label="处置说明" :value="item?.yssm" />
              </div>
            </template>
          </div>

          <!-- 处方 -->
          <template v-if="list?.prescription">
            <BaseTitle title="处方" />
            <div v-for="(item, indexPrescription) in list.prescription" :key="indexPrescription" class="mb-3">
              <a-divider v-if="indexPrescription > 0" class="my-2" dashed />
              <div class="flex justify-between mb-1">
                <span>处方{{ indexPrescription + 1 }}</span>
                <a-button :disabled="!list?.prescription?.length" type="primary" size="small" ghost @click="onCopy(3, item)">复制</a-button>
              </div>
              <template v-if="item.children && item.children.length > 0">
                <div v-for="(itemParent, indexParent) in item.children" :key="indexParent">
                  <div class="flex justify-between">
                    <LabelValue :value="itemParent?.mc" :width="90" />
                    <LabelValue :value="itemParent?.gg" :width="90" />
                    <LabelValue :value="itemParent?.jlsl" :width="90" class="text-right" />
                  </div>
                  <div class="flex justify-between">
                    <LabelValue :value="itemParent?.fyff" />
                    <LabelValue :value="itemParent?.pl" />
                    <LabelValue :value="itemParent?.plsl" class="text-right" />
                  </div>
                  <LabelValue label="医生说明" class="mb-3" :value="item?.yssm" />
                  <template v-if="itemParent.mxlist && itemParent.mxlist.length > 0">
                    <div v-for="(itemChild, indexChild) in itemParent.mxlist" :key="indexChild">
                      <a-divider dashed class="son-doctor-advice">子医嘱{{ indexChild + 1 }}</a-divider>
                      <div class="flex justify-between">
                        <LabelValue :value="itemChild?.mc" :width="90" />
                        <LabelValue :value="itemChild?.gg" :width="90" />
                        <LabelValue :value="itemChild?.jlsl" :width="90" class="text-right" />
                      </div>
                      <div class="flex justify-between">
                        <LabelValue :value="itemChild?.fyff" />
                        <LabelValue :value="itemChild?.pl" />
                        <LabelValue :value="itemChild?.plsl" class="text-right" />
                      </div>
                      <LabelValue label="医生说明" class="mb-3" :value="itemChild?.yssm" />
                      <a-divider dashed class="!h-0 !my-3" />
                    </div>
                  </template>
                  <template v-else>
                    <a-divider dashed class="!h-0 !my-3" />
                  </template>
                </div>
              </template>
            </div>
          </template>

          <template v-if="list?.outpTCMHeadList">
            <BaseTitle title="中药处方">
              <a-button type="primary" size="small" @click="onExportTCM()">导出</a-button>
            </BaseTitle>
            <template v-if="list?.outpTCMHeadList">
              <div v-for="(item, index) in list.outpTCMHeadList" :key="index" class="mb-3">
                <a-divider v-if="index > 0" class="my-2" dashed />
                <div class="flex justify-between">
                  <span>中药处方{{ index + 1 }}</span>
                  <a-button type="primary" size="small" ghost @click="onCopy(4, item)">复制</a-button>
                </div>
                <template v-if="item.children?.length > 0">
                  <div v-for="(itemDrug, indexDrug) in item.children" :key="indexDrug" class="flex justify-between mt-1">
                    <LabelValue :value="itemDrug?.mc" :width="100" />
                    <LabelValue :value="itemDrug?.jf" class="flex-1 text-center" />
                    <LabelValue :value="itemDrug?.sl + 'g'" class="flex-1 text-right" />
                  </div>
                </template>
                <div class="flex justify-between mt-1">
                  <LabelValue :value="item?.fssl + '付'" :width="60" />
                  <LabelValue :value="item?.yf" :width="70" />
                  <LabelValue :value="item?.pl" :width="70" />
                  <LabelValue :value="item?.jcl" :width="80" />
                </div>
                <div class="flex justify-end mt-1">
                  <LabelValue :value="'￥' + item?.zjjg" class="text-right" />
                </div>
              </div>
            </template>
          </template>
        </div>
      </a-collapse-panel>
    </a-collapse>
    <a-empty v-else />
  </a-spin>
</template>
<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import BaseTitle from '@/components/BaseTitle'
import LabelValue from '@/components/LabelValue'
import { post } from '@/utils/request'
import useStore from '@/views/out-doctor/store'

const emit = defineEmits(['success'])
const store = useStore()
const dataSource = ref([])
const copyType = ref()
const copyTypeActive = ref()
const copyRow = ref({})

const loading = ref(true)
const queryList = async () => {
  loading.value = true
  const res = await post('/outp/mzys/getPatientEMRInfo', {
    brId: store.currentPatient.brId
  })
  if (res.code !== 0) return message.error(res.msg || '请求失败')
  loading.value = false
  dataSource.value = res.data || []
}
// onMounted(() => {
//   store.currentPatient.brId && queryList()
// })
watchEffect(() => {
  store.currentPatient.brId && queryList()
})
const onSaveCopy = async () => {
  const params = {
    cfbh: copyRow.value.cfbh || '', // 西药处方编号
    cfdh: copyRow.value.cfdh || '', // 中药处方单号
    copyType: copyType.value,
    pastJzbh: copyRow.value.jzbh, // 原就诊编号
    rightNowJzbh: store.currentPatient.jzbh // 现就诊编号
  }
  if (copyType.value === 2) {
    params.pastJzbh = copyRow.value.children[0]?.jzbh // 原就诊编号
  }
  const res = await post('/outp/mzys/copyMedicalRecord', params)
  if (res.code !== 0) return message.error(res.msg || '请求失败')
  message.success('复制成功')
  emit('success', copyTypeActive.value)
}

const onCopy = (type, list) => {
  copyRow.value = list
  switch (type) {
    case 1:
      // copyType.value = 0
      store.tempCase = list
      copyTypeActive.value = '1'
      break
    case 2:
      copyType.value = 1
      copyTypeActive.value = '2'
      break
    case 3:
      copyType.value = 2
      copyTypeActive.value = '3'
      break
    case 4:
      copyType.value = 3
      copyTypeActive.value = '4'
      break
    default:
      break
  }
  type !== 1 && onSaveCopy()
}
const onExportTCM = async () => {
  //TODO
  // const res = await post('/outp/mzys/exportTCM', {
  //   brId: store.currentPatient.brId
  // })
  // if (res.code !== 0) return message.error(res.msg || '请求失败')
  // const url = window.URL.createObjectURL(new Blob([res.data]))
  // const link = document.createElement('a')
  // link.href = url
  // link.setAttribute('download', '中药处方.pdf')
  // document.body.appendChild(link)
  // link.click()
  // document.body.removeChild(link)
}
</script>

<style lang="less" scoped>
.doctor-advice {
  :deep(.ant-collapse-header) {
    padding: 0;
  }
  .advice-son {
    color: #525252;
    font-size: 13px;
  }
}
.son-doctor-advice {
  height: 12px;
  color: var(--primary-color);
  font-size: 14px;
}
:deep(.ant-divider-dashed::before) {
  border-color: #ccc;
  border-width: 1px;
}
:deep(.ant-divider-dashed::after) {
  border-color: #ccc;
  border-width: 1px;
}
</style>
